﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Drawing;

public partial class Categories : System.Web.UI.Page
{

    SqlConnection con = new SqlConnection();
    SqlDataAdapter da;
    DataSet ds;
    SqlCommandBuilder cb;
    DataTable dt;
    DataView dv;
    protected void Page_Load(object sender, EventArgs e)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        con.ConnectionString = constr;
        con.Open();
        if (this.IsPostBack == false)
        {
            LoadCategoryInfo();


        }

        btnUpdate.Enabled = false;
        btnDelete.Enabled = false;

    }

    void LoadCategoryInfo()
    {
        da = new SqlDataAdapter("SELECT * FROM Production.Categories", con);

        dt = new DataTable("Production.Categories");

        da.Fill(dt);

        gvCategory.AutoGenerateSelectButton = true;
        gvCategory.AllowPaging = true;
        gvCategory.PageSize = 5;

        gvCategory.DataSource = dt;
        gvCategory.DataBind();

    }
    protected void gvCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnUpdate.Enabled = true;
        btnDelete.Enabled = true;
        btnAdd.Enabled = false;

        GridViewRow r = gvCategory.SelectedRow;

        int index = (int)Session["Category_index"];

        if (gvCategory.SelectedRow.RowIndex != index && index != -1)
        {
            gvCategory.Rows[index].BackColor = Color.White;

        }
        index = gvCategory.SelectedRow.RowIndex;
        Session["Category_index"] = index;
        r.BackColor = Color.Yellow;
        txtID.Text = r.Cells[1].Text;
        txtName.Text = r.Cells[2].Text;
        txtDescription.Text = r.Cells[3].Text;

    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        txtID.Text = "";
        txtName.Text = "";
        txtDescription.Text = "";
        LoadCategoryInfo();
        txtSearch.Text = "";
        gvCategory.BackColor = Color.White;
        btnAdd.Enabled = true;
        btnUpdate.Enabled = false;
        btnDelete.Enabled = false;
        Session["Category_index"] = -1;
        Session["Category_Search"] = false;
    }

    void add()
    {
        try
        {
            
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "AddCategory";

            SqlParameter param = new SqlParameter("@categoryName", SqlDbType.NVarChar);
            param.Size = 15;
            param.Value = txtName.Text;
            cmd.Parameters.Add(param);

            param = new SqlParameter("@description", SqlDbType.NVarChar);
            param.Size = 200;
            param.Value = txtDescription.Text;
            cmd.Parameters.Add(param);

            cmd.ExecuteNonQuery();
            Response.Write("<script language='JavaScript'> alert('ADD SUCCESSFULLY!'); </script>");
        }
        catch (Exception ex)
        {
            
            //Response.Write("<script language='JavaScript'> alert('ADD FAILED!'); </script>");

        }
        finally
        {

        
        }
            
        
    }

    void update()
    {
        try
        {
           
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "UpdateCategory";

            SqlParameter param = new SqlParameter("@categoryName", SqlDbType.NVarChar);
            param.Size = 15;
            param.Value = txtName.Text;
            cmd.Parameters.Add(param);

            param = new SqlParameter("@description", SqlDbType.NVarChar);
            param.Size = 200;
            param.Value = txtDescription.Text;
            cmd.Parameters.Add(param);

            param = new SqlParameter("@categoryID", SqlDbType.Int);
            param.Value = int.Parse(txtID.Text.ToString());
            cmd.Parameters.Add(param);

            cmd.ExecuteNonQuery();
            Response.Write("<script language='JavaScript'> alert('UPDATE SUCCESSFULLY!'); </script>");
        }
        catch (Exception ex)
        {
            Response.Write("<script language='JavaScript'> alert('UPDATE FAILED!'); </script>");

        }

        finally
        {
            
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        add();
        Session["Category_index"] = -1;
        Session["Category_Search"] = false;
        LoadCategoryInfo();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        update();
        Session["Category_index"] = -1;
        Session["Category_Search"] = false;
        LoadCategoryInfo();
    }

    void delete()
    {

        try
        {
            
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "DeleteCategory";

            SqlParameter param = new SqlParameter("@categoryID", SqlDbType.Int);
            param.Value = int.Parse(txtID.Text.ToString());
            cmd.Parameters.Add(param);

            cmd.ExecuteNonQuery();
            Response.Write("<script language='JavaScript'> alert('DELETE SUCCESSFULLY!'); </script>");

        }
        catch (Exception e)
        {
            Response.Write("<script language='JavaScript'> alert('CANNOT DELETE!'); </script>");

        }
        finally
        {
           
        }
       
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        delete();
        Session["Category_index"] = -1;
        Session["Category_Search"] = false;
        LoadCategoryInfo();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        LoadCategoryInfo();
        Session["Category_Search"] = true;
        dv = new DataView(dt);
        dv.RowFilter = string.Format("categoryname like '%{0}%' ", txtSearch.Text);
        gvCategory.DataSource = dv;
        gvCategory.DataBind();
        
        Session["Category_index"] = -1;
    }
   
    protected void gvCategory_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        LoadCategoryInfo();

        if ((bool)Session["Category_Search"] == true)
        {
            gvCategory.PageIndex = e.NewPageIndex;
            dv = new DataView(dt);
            dv.RowFilter = string.Format("categoryname like '%{0}%' ", txtSearch.Text);
            gvCategory.DataSource = dv;
            gvCategory.DataBind();

        }
        else
        {
            gvCategory.PageIndex = e.NewPageIndex;
            gvCategory.DataBind();
        }
    }
}
